<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :class="editType =='detail'?'pic-preview':''"
    :cancelText="editType =='detail'?'关闭':'取消'">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="统一信用代码">
          <a-input @blur="findCode" :disabled="editType=='detail'" placeholder="统一信用代码" v-decorator="['CompanyCode', {rules:[{required:true,message:'请输入'}]}]" />
            
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机构全称">
          <a-input @blur="findCode" :disabled="editType=='detail'" placeholder="请输入机构全称" v-decorator="['CompanyName', {rules:[{required:true,message:'请输入'}]}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机构简称">
          <a-input :disabled="editType=='detail'" placeholder="请输入机构简称" v-decorator="['CompanyAbbr']" />
        </a-form-item>
        <a-form-item 
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="机构介绍">
          <a-input :disabled="editType=='detail'" type="textarea" placeholder="请输入一句话简介" v-decorator="['CompanyBrief', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关注领域">
          <a-input :disabled="editType=='detail'" placeholder="请输入关注领域" v-decorator="['Aoc']" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="投资阶段">
          <a-input :disabled="editType=='detail'" placeholder="请输入投资阶段" v-decorator="['InvestmentStage']" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="相关组织">
          <!-- <a-input :disabled="editType=='detail'" placeholder="请输入公司简称" v-decorator="['RelationCompanyId']" /> -->
          <a-input placeholder="请选择公司" v-show v-decorator="['RelationCompanyId']"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['RelationCompanyName',{}]"></a-input>
        </a-form-item>
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上级园区">
          <a-select placeholder="请选择上级园区" v-decorator="['UpParkId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseHighteckParkId" v-for="(item,index) in ParkListData">{{item.ParkName}}</a-select-option>
          </a-select>
        </a-form-item> -->
        
		
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      selectType="checkbox"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRowsForInvestor"
      :searchArr="searchArr"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "BaseHighteckparkModal",
    components:{
      commonModal
    },
    watch:{
      visible(newVal,oldVal){
        if(newVal){
          this.editType = sessionStorage.getItem('editType');
        }
      }
    },
    data () {
      return {
        editType:'',
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        ParkListData:[],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        ParkTypeData:[],
        url: {
          add: "/companyInfo/add",
          edit: "/companyInfo/update",
          baseList:"/baseInfo/queryRows",
          list:'/companyInfo/queryRows'
        },
      }
    },
    created () {
    },
    methods: {
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        let ids = [];
        let names = [];
        val.forEach(e=>{
          ids.push(e.CompanyId);
          names.push(e.CompanyName);
        })
        this.form.setFieldsValue({
          RelationCompanyId:ids.join(),
          RelationCompanyName:names.join()
        })
      },
      findCode(){
        this.$nextTick(()=>{
          let data = this.form.getFieldsValue();
          let arr = [];
          if(data.CompanyCode){
            arr.push({"field":"CompanyCode",
              "operator":"=",
              "value":data.CompanyCode
            });
          }else if(data.CompanyName){
            arr.push({"field":"CompanyName",
              "operator":"=",
              "value":data.CompanyName
            });
          }
          this.$http.postAction('/companyInfo/queryRows',{pageNo:1,pageSize:1,selectFields:[],conditions:{OR:arr}}).then(res=>{
              if(res.status == 1 && 0 < res.data.rows.length){
                if(res.data.rows[0].CompanyId == this.model.CompanyId){
                  return
                }
                this.$message.warning('信用代码或公司全称重复录入');
              }
            })
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.model.RelationCompanyId = this.model.RelationCompany || '';
        this.model.RelationCompanyName = this.model.RelationCompany_CompanyName || '';
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'CompanyCode','CompanyName','CompanyAbbr','CompanyBrief','InvestmentStage','Aoc','RelationCompanyId','RelationCompanyName'))
          
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign({CompanyId:this.model.CompanyId}, values);
            //时间格式化
            
            console.log(formData)
            formData.CompanyType = 1;
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>